//package cn.vc.gateway.filter;
//
//import cn.vc.gateway.util.EncryptUtil;
//import com.alibaba.fastjson.JSON;
//import com.netflix.zuul.ZuulFilter;
//import com.netflix.zuul.context.RequestContext;
//import com.netflix.zuul.exception.ZuulException;
//import jdk.internal.dynalink.linker.LinkerServices;
//import org.apache.http.protocol.RequestContent;
//import org.bouncycastle.cert.ocsp.Req;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.oauth2.provider.OAuth2Authentication;
//import org.springframework.security.oauth2.provider.OAuth2Request;
//
//import java.util.*;
//
///**
// * @author mo
// * @Description:
// * @日期: 2024-04-05 11:01
// **/
//@Configuration
//public class AuthFilter extends ZuulFilter {
//    @Override
//    public String filterType() {
//        return "pre";
//    }
//
//    @Override
//    public int filterOrder() {
//        return 0;
//    }
//
//    @Override
//    public boolean shouldFilter() {
//        return true;
//    }
//
//    @Override
//    public Object run() throws ZuulException {
//        RequestContext ctx = RequestContext.getCurrentContext();
//        // 从安全上下文中拿到用户身份对象
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        if (!(authentication instanceof OAuth2Authentication)) {
//            return null;
//        }
//        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
//        Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
//        // 获取当前用户身份信息
//        String principal = userAuthentication.getName();
//        // 获取当前用户权限信息
//        List<String> authorities = new ArrayList<>();
//        // 从userAuthentication取出权限，放在authorities
//        userAuthentication.getAuthorities().stream().forEach(c -> {
//            authorities.add(((GrantedAuthority)c).getAuthority());
//        });
//
//        OAuth2Request oAuth2Request = oAuth2Authentication.getOAuth2Request();
//        Map<String, String> requestParams = oAuth2Request.getRequestParameters();
//        Map<String, Object> jsonToken = new HashMap<>(requestParams);
//        if (userAuthentication!=null) {
//            jsonToken.put("principal", principal);
//            jsonToken.put("authorities", authorities);
//        }
//
//        // 把身份信息和权限信息放在json当中，加入http的header中
//        ctx.addZuulRequestHeader("json-token", EncryptUtil.encodeUTF8StringBase64(JSON.toJSONString(jsonToken)));
//
//        return null;
//    }
//}
